package com.qingzhuge.common.email;


/**
 * @author : zero.xiao
 * @description : 发送邮件辅助类
 * @modified :
 */
public final class EmailUtil {
    private EmailUtil() {
    }

    /**
     * 发送邮件
     */
    public static boolean sendEmail(Email email) {
        EmailSender sender = new EmailSender(email.getHost(), email.getPort(), email.isSsl());
        if (!sender.setNamePass(email.getName(), email.getPassword(), email.getUserkey())) {
            return false;
        }
        if (!sender.setFrom(email.getFrom())) {
            return false;
        }
        if (!sender.setTo(email.getSendTo())) {
            return false;
        }
        if (email.getCopyTo() != null && !sender.setCopyTo(email.getCopyTo())) {
            return false;
        }
        if (!sender.setSubject(email.getTopic())) {
            return false;
        }
        if (!sender.setBody(email.getBody())) {
            return false;
        }
        if (email.getFileAffix() != null) {
            for (int i = 0; i < email.getFileAffix().length; i++) {
                if (!sender.addFileAffix(email.getFileAffix()[i])) {
                    return false;
                }
            }
        }
        // 发送
        return sender.sendout();
    }
}
